﻿using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.BackgroundJobs;
using XDQ.VAT.Lockers;
using XDQ.VAT.Students;

namespace XDQ.VAT.Web.BackgroundJobs
{
    public class DailySyncAgeJob : AsyncBackgroundJob<DailySyncAgeJobArg>, Volo.Abp.DependencyInjection.ITransientDependency
    {
        public DailySyncAgeJob(IAppStudentAppService studentAppService, IDistributeLocker distributeLocker)
        {
            StudentAppService = studentAppService;
            DistributeLocker = distributeLocker;
        }

        public IAppStudentAppService StudentAppService { get; }
        public IDistributeLocker DistributeLocker { get; }

        public override async Task ExecuteAsync(DailySyncAgeJobArg args)
        {
            if (await DistributeLocker.TryLock<AppStudent>())
            {
                await StudentAppService.SyncStudentAgeAsync();
            }
            else
            {
                Logger.LogWarning($"尝试抢占学生信息的锁失败，跳过执行同步年龄的任务~！");
            }
        }
    }
}
